package com.isunland.managesystem.entity;

import android.content.Context;
import android.text.TextUtils;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.SharedPreferencesUtil;

/**
 * 程序信息保存类.
 */
@SuppressWarnings("unused") public class CurrentUser {
  private static final String TAG = CurrentUser.class.getSimpleName();
  public static final String NODE_CLASS = "nodeclass";
  public static final String DEPT_CLASS = "deptclass";
  private static CurrentUser sCurrentUser;
  /** 管理单位部门名称 */
  private String manageOrgName;
  /** 管理单位部门编码 */
  private String manageOrgCode;
  private String userId;
  private String memberCode;
  private String password;
  private String jobNumber;// 工号
  private String picture;// 头像地址
  private Context mAppContext;
  /**
   * 登录名
   */
  private String name;
  /**
   * 真名
   */
  private String realName;
  /**
   * 电话
   */
  private String mobile;
  /**
   * 邮箱
   */
  private String email;
  /**
   * 公司名称
   */
  private String memberName;
  /**
   * 职务
   */
  private String keypositionName;
  /***
   * 部门名称
   */
  private String deptName;
  private String deptCode;
  private String birthDate;
  private String phoneIdentifyNumber;// 手机标识
  private String pushClientId;// 推送id
  private boolean isLogin;//是否在登陆状态，本地做判断。

  public String getManageOrgName() {
    return manageOrgName;
  }

  public String getManageOrgCode() {
    return manageOrgCode;
  }

  private CurrentUser(Context context) {
    super();
    mAppContext = context;
    //初始化的时候从sp中获取,在赋值给成员变量.
    String name = SharedPreferencesUtil.getString(context, SharedPreferencesUtil.USERNAME, "");
    String password = SharedPreferencesUtil.getString(context, SharedPreferencesUtil.PASSWORD, "");
    String userId = SharedPreferencesUtil.getString(context, SharedPreferencesUtil.USER_ID, "");
    String memberCode =
        SharedPreferencesUtil.getString(context, SharedPreferencesUtil.MEMBER_CODE, "");
    String realName = SharedPreferencesUtil.getString(context, SharedPreferencesUtil.NAME, "");
    String jobNumer =
        SharedPreferencesUtil.getString(context, SharedPreferencesUtil.JOB_NUMBER, "");
    String picture = SharedPreferencesUtil.getString(context, SharedPreferencesUtil.PICTURE, "");
    String phoneIdentifyNumber =
        SharedPreferencesUtil.getString(context, SharedPreferencesUtil.MOBILE_IMIS, "");
    String pushClientId =
        SharedPreferencesUtil.getString(context, SharedPreferencesUtil.PUSH_CLIENT_ID, "");
    String deptCode = SharedPreferencesUtil.getString(context, SharedPreferencesUtil.DEPT_CODE, "");
    String deptName = SharedPreferencesUtil.getString(context, SharedPreferencesUtil.DEPT_NAME, "");
    String keypositionName =
        SharedPreferencesUtil.getString(context, SharedPreferencesUtil.KEY_POSITION_NAME, "");
    String email = SharedPreferencesUtil.getString(context, SharedPreferencesUtil.EMIAL, "");
    String memberName =
        SharedPreferencesUtil.getString(context, SharedPreferencesUtil.MEMBER_NAME, "");
    String mobile = SharedPreferencesUtil.getString(context, SharedPreferencesUtil.MOBILE, "");
    String manageOrgCode =
        SharedPreferencesUtil.getString(context, SharedPreferencesUtil.MANAGE_ORG_CODE, "");
    String manageOrgName =
        SharedPreferencesUtil.getString(context, SharedPreferencesUtil.MANAGE_ORG_NAME, "");
    boolean isLogin =
        SharedPreferencesUtil.getBoolean(context, SharedPreferencesUtil.IS_LOGIN, false);
    this.name = name;
    this.password = password;
    this.realName = realName;
    this.jobNumber = jobNumer;
    this.userId = userId;
    this.memberCode = memberCode;
    this.picture = picture;
    this.phoneIdentifyNumber = phoneIdentifyNumber;
    this.pushClientId = pushClientId;
    this.deptCode = deptCode;
    this.deptName = deptName;
    this.keypositionName = keypositionName;
    this.email = email;
    this.memberName = memberName;
    this.mobile = mobile;
    this.manageOrgCode = manageOrgCode;
    this.manageOrgName = manageOrgName;
    this.isLogin = isLogin;
  }

  public static CurrentUser newInstance(Context context) {
    if (sCurrentUser == null) {

      sCurrentUser = new CurrentUser(context.getApplicationContext());
    }

    return sCurrentUser;
  }

  /**
   * 与单例,sp文件同步
   */
  public void update(String mUsername, String mPassword, String realName, String userId,
      String memberCode, String jobNumber, String picture, String phoneIdentifyNumber,
      String deptCode, String pushClientId, String deptName, String keypositionName, String email,
      String memberName, String mobile, String manageOrgCode, String manageOrgName,
      boolean isLogin) {
    this.name = mUsername;
    this.password = mPassword;
    this.realName = realName;
    this.userId = userId;
    this.memberCode = memberCode;
    this.jobNumber = jobNumber;
    this.picture = picture;
    this.phoneIdentifyNumber = phoneIdentifyNumber;
    this.deptCode = deptCode;
    this.pushClientId = pushClientId;
    this.deptName = deptName;
    this.keypositionName = keypositionName;
    this.email = email;
    this.memberName = memberName;
    this.mobile = mobile;
    this.manageOrgCode = manageOrgCode;
    this.manageOrgName = manageOrgName;
    this.isLogin = isLogin;
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.USERNAME, mUsername);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.PASSWORD, mPassword);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.NAME, realName);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.USER_ID, userId);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.MEMBER_CODE, memberCode);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.JOB_NUMBER, jobNumber);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.PICTURE, picture);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.MOBILE_IMIS,
        phoneIdentifyNumber);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.ORG_CODE, deptCode);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.PUSH_CLIENT_ID,
        pushClientId);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.DEPT_NAME, deptName);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.KEY_POSITION_NAME,
        keypositionName);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.EMIAL, email);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.MEMBER_NAME, memberName);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.MOBILE, mobile);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.MANAGE_ORG_CODE,
        manageOrgCode);
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.MANAGE_ORG_NAME,
        manageOrgName);
    SharedPreferencesUtil.setBoolean(mAppContext, SharedPreferencesUtil.IS_LOGIN, isLogin);
  }

  public boolean isLogin() {
    return isLogin;
  }

  public void setLogin(boolean login) {
    this.isLogin = login;
    SharedPreferencesUtil.setBoolean(mAppContext, SharedPreferencesUtil.IS_LOGIN, isLogin);
  }

  public String getPhoneIdentifyNumber() {
    return phoneIdentifyNumber;
  }

  //public String getPushClientId() {
  //    return pushClientId;
  //}

  //public void setPushClientId(String pushClientId) {
  //    this.pushClientId = pushClientId;
  //    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.PUSH_CLIENT_ID, pushClientId);
  //}

  public void setPhoneIdentifyNumber(String phoneIdentifyNumber) {

    this.phoneIdentifyNumber = phoneIdentifyNumber;
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.MOBILE_IMIS,
        phoneIdentifyNumber);
  }

  public String getDeptName() {
    return deptName;
  }

  public String getDeptCode() {
    return deptCode;
  }

  //private void setDeptName(String deptName) {
  //    this.deptName = deptName;
  //}

  public String getBirthDate() {
    return birthDate;
  }

  //private void setDeptCode(String deptCode) {
  //    this.deptCode = deptCode;
  //}

  public String getMobile() {
    return mobile;
  }

  //private void setBirthDate(String birthDate) {
  //    this.birthDate = birthDate;
  //}

  public String getEmail() {
    return email;
  }

  //private void setMobile(String mobile) {
  //    this.mobile = mobile;
  //}

  public String getMemberName() {
    return memberName;
  }

  //private void setEmail(String email) {
  //    this.email = email;
  //}

  public String getKeypositionName() {
    return keypositionName;
  }

  //private void setMemberName(String memberName) {
  //    this.memberName = memberName;
  //}

  public String getRealName() {
    return realName;
  }

  //private void setKeypositionName(String keypositionName) {
  //    this.keypositionName = keypositionName;
  //}

  public String getJobNumber() {
    return jobNumber;
  }

  //private void setRealName(String realName) {
  //    this.realName = realName;
  //}

  public String getName() {
    return name;
  }

  //private void setJobNumber(String jobNumber) {
  //    this.jobNumber = jobNumber;
  //}

  public void setName(String name) {
    this.name = name;
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.USERNAME, name);
  }

  public void setProvince(String province) {
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.PROVINCE, province);
  }

  public void setRoleTypeFlag(String roleTypeFlag) {
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.ROLE_TYPE_FLAG,
        roleTypeFlag);
  }

  public void setLatitude(String latitude) {
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.LATITUDE, latitude);
  }

  public void setLongitude(String longitude) {
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.LONGITUDE, longitude);
  }

  public void setAddress(String address) {
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.ADDRESS, address);
  }
  //public void setProvinceCode(String provinceCode) {
  //  SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.PROVINCE_CODE, provinceCode);
  //}

  public String getProvince() {
    return SharedPreferencesUtil.getString(mAppContext, SharedPreferencesUtil.PROVINCE, "");
  }

  public String getRoleTypeFlag() {
    return SharedPreferencesUtil.getString(mAppContext, SharedPreferencesUtil.ROLE_TYPE_FLAG, "");
  }

  public String getLatitude() {
    return SharedPreferencesUtil.getString(mAppContext, SharedPreferencesUtil.LATITUDE, "");
  }

  public String getLongitude() {
    return SharedPreferencesUtil.getString(mAppContext, SharedPreferencesUtil.LONGITUDE, "");
  }

  public String getAddress() {
    return SharedPreferencesUtil.getString(mAppContext, SharedPreferencesUtil.ADDRESS, "");
  }
  //public String getProvinceCode() {
  // return SharedPreferencesUtil.getString(mAppContext, SharedPreferencesUtil.PROVINCE_CODE, "");
  //}

  //private void setName(String name) {
  //    this.name = name;
  //}

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.PASSWORD, password);
  }

  //public boolean isExist() {
  //    return !(name == null || name.isEmpty());
  //}

  public String getUserId() {
    return userId;
  }

  //private void setUserId(String userId) {
  //    this.userId = userId;
  //}

  public String getMemberCode() {
    return memberCode;
  }

  //private void setMemberCode(String memberCode) {
  //    this.memberCode = memberCode;
  //}

  public String getPicture() {
    return picture;
  }

  public void setPicture(String picture) {
    this.picture = picture;
    SharedPreferencesUtil.setString(mAppContext, SharedPreferencesUtil.PICTURE, picture);
  }

  public boolean isCreater(String id) {
    if (TextUtils.isEmpty(id)) {
      return false;
    }
    LogUtil.i(TAG, "id=" + id + ",jobNo=" + jobNumber);
    return id.equalsIgnoreCase(jobNumber);
  }

  @Override public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("name=")
        .append(name)
        .append(",")
        .append("realName=")
        .append(realName)
        .append(",")
        .append("mobile=")
        .append(mobile)
        .append(",")
        .append("email=")
        .append(email)
        .append(",")
        .append("memberName=")
        .append(memberName)
        .append(",")
        .append("keypositionName=")
        .append(keypositionName)
        .append(".")
        .append(phoneIdentifyNumber)
        .append(".")
        .append(deptCode)
        .append(".");
    LogUtil.i(TAG, "userInfo=" + sb.toString());
    return sb.toString();
  }

  /**
   * 清空用户信息,用于退出登陆等操作.
   */
  public void clear() {
    update("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", false);
  }
}
